package cn.uin.trigger.http;

import cn.uin.api.IOssService;
import cn.uin.api.dto.response.oss.UploadFileResponse;
import cn.uin.api.response.Response;
import cn.uin.domain.oss.model.entity.HomeAttachmentEntity;
import cn.uin.domain.oss.service.file.IFileService;
import cn.uin.types.enums.ResponseCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

/**
 * @ClassName FileController
 * @Description:文件服务接口
 * @Author: Administrator
 * @CreateDate: 2025/11/5 16:06
 * @Version: 1.0
 */
@Slf4j
@RestController()
@CrossOrigin("*")
@RequestMapping("/api/file/")
public class OssController implements IOssService {

	@Resource
	private IFileService iFileService;

	@Override
	@RequestMapping(value = "uploadFile", method = RequestMethod.POST)
	public Response<UploadFileResponse> uploadFile(@RequestParam("file") MultipartFile file, @RequestParam("uploadType") Integer uploadType) {
		HomeAttachmentEntity homeAttachmentEntity = iFileService.uploadFile(file, uploadType);
		UploadFileResponse uploadFileResponse = UploadFileResponse.builder().build();
		BeanUtils.copyProperties(homeAttachmentEntity, uploadFileResponse);
		return Response.<UploadFileResponse>builder()
				.code(ResponseCode.SUCCESS.getCode())
				.info(ResponseCode.SUCCESS.getInfo())
				.data(uploadFileResponse)
				.build();
	}

	@Override
	@RequestMapping(value = "getFileUrl", method = RequestMethod.POST)
	public Response<String> getFileUrl(Long fileId, Integer uploadType) {
		String fileUrl = iFileService.getFileUrl(fileId, uploadType);
		return Response.<String>builder()
				.code(ResponseCode.SUCCESS.getCode())
				.info(ResponseCode.SUCCESS.getInfo())
				.data(fileUrl)
				.build();
	}
}
